﻿using Star.Tools.Helper;
using System;
using System.IO;

namespace Star.Tools.Files {
    /// <summary>
    /// 文件大小
    /// </summary>
    public struct FileSize {

        #region 获取一个文件的长度
        /// <summary>
        /// 获取一个文件的长度,单位为
        /// </summary>
        /// <param name="filePath">文件的绝对路径</param>
        /// <param name="unit">字节单位</param>
        public static double GetFileSize(string filePath, string unit = null)
        {
            System.IO.FileInfo fi = new System.IO.FileInfo(filePath);
            if (unit == "KB")
            {
                return Convert.ToDouble(fi.Length) / 1024;
            }
            else if (unit == "MB")
            {
                return Convert.ToDouble(fi.Length) / 1024 / 1024;
            }
            return (double)fi.Length;
        }

        /// <summary>
        /// 将文件大小转换输出
        /// </summary>
        /// <param name="Size"></param>
        /// <returns></returns>
        public static string GetFileSize(long Size)
        {
            string m_strSize = "";
            long FactSize = 0;
            FactSize = Size;
            if (FactSize < 1024.00)
                m_strSize = FactSize.ToString() + "Byte";
            else if (FactSize >= 1024.00 && FactSize < 1048576)
                m_strSize = (FactSize / 1024.00).ToString("F2") + "K";
            else if (FactSize >= 1048576 && FactSize < 1073741824)
                m_strSize = (FactSize / 1024.00 / 1024.00).ToString("F2") + "M";
            else if (FactSize >= 1073741824)
                m_strSize = (FactSize / 1024.00 / 1024.00 / 1024.00).ToString("F2") + "G";

            return m_strSize;
        }
        #endregion 获取一个文件的长度
        /// <summary>
        /// 初始化文件大小
        /// </summary>
        /// <param name="size">文件字节大小</param>
        /// <param name="unit">文件字节大小</param>
        public FileSize(long size, FileSizeUnit unit = FileSizeUnit.Byte) => Size = GetSize(size, unit);


        /// <summary>
        /// 获取文件大小
        /// </summary>
        private static long GetSize( long size, FileSizeUnit unit ) {
            switch ( unit ) {
                case FileSizeUnit.K:
                    return size * 1024;
                case FileSizeUnit.M:
                    return size * 1024 * 1024;
                case FileSizeUnit.G:
                    return size * 1024 * 1024 * 1024;
                default :
                    return size;
            }
        }

        /// <summary>
        /// 文件字节长度
        /// </summary>
        public long Size { get; }

        /// <summary>
        /// 获取文件大小，单位：G
        /// </summary>
        public double GetSizeByG() {
            return Math.Round(Convert.ToDouble(Size / 1024.0 / 1024.0 / 1024.0), 2);
        }

        /// <summary>
        /// 获取文件大小，单位：M
        /// </summary>
        public double GetSizeByM() {
            return Math.Round(Convert.ToDouble(Size / 1024.0 / 1024.0), 2);
        }

        /// <summary>
        /// 获取文件大小，单位：K
        /// </summary>
        public double GetSizeByK() {
            return Math.Round(Convert.ToDouble(Size / 1024.0), 2 );
        }

        /// <summary>
        /// 获取文件大小，单位：字节
        /// </summary>
        public int GetSize() => (int)Size;

        ///// <summary>
        ///// 输出描述
        ///// </summary>
        //public override string ToString()
        //{
        //    if (_size >= 1024 * 1024 * 1024)
        //        return string.Format("{0} {1}", GetSizeByG(), EnumHelper.GetDescription(FileUnit.G));
        //    if (_size >= 1024 * 1024)
        //        return string.Format("{0} {1}", GetSizeByM(), EnumHelper.GetDescription(FileUnit.M));
        //    if (_size >= 1024)
        //        return string.Format("{0} {1}", GetSizeByK(), EnumHelper.GetDescription(FileUnit.K));
        //    return string.Format("{0} {1}", _size, EnumHelper.GetDescription(FileUnit.Byte));
        //}
        /// <summary>
        /// 输出描述
        /// </summary>
        public override string ToString()
        {
            if (Size >= 1024 * 1024 * 1024)
                return $"{GetSizeByG()} {EnumHelper.GetDescription(FileSizeUnit.G)}";
            if (Size >= 1024 * 1024)
                return $"{GetSizeByM()} {EnumHelper.GetDescription(FileSizeUnit.M)}";
            if (Size >= 1024)
                return $"{GetSizeByK()} {EnumHelper.GetDescription(FileSizeUnit.K)}";
            return $"{GetSize()} {EnumHelper.GetDescription(FileSizeUnit.Byte)}";
        }
    }
}
